package com.learner.ucenter.controller.api;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.common.util.JwtUtils;
import com.learner.pojo.user.User;
import com.learner.pojo.user.vo.GroupMessageResultVo;
import com.learner.pojo.user.vo.MessageListVo;
import com.learner.pojo.user.vo.MyFriendListResultVo;
import com.learner.pojo.user.vo.MyGroupResultVo;
import com.learner.pojo.user.vo.SingleMessageResultVo;
import com.learner.ucenter.repository.UserDao;
import com.learner.ucenter.service.GoodFriendService;
import com.learner.ucenter.service.GroupMessageService;
import com.learner.ucenter.service.GroupService;
import com.learner.ucenter.service.GroupUserService;
import com.learner.ucenter.service.SingleMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author ice2020x
 * @Date: 2023/2/27
 * @Description: 消息联查控制类
 **/
@RequestMapping("/api/ucenter/message")
@RestController
@Slf4j
public class MessageController {

    @Autowired
    private SingleMessageService singleMessageService;

    @Autowired
    private GroupMessageService groupMessageService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private GroupUserService groupUserService;

    @Autowired
    private GoodFriendService goodFriendService;

    @Autowired
    private UserDao userDao;

    /**
     * 获取我的消息列表
     *
     * @param request request
     * @return 获取我的消息列表
     */
    @GetMapping("/messageListItem")
    public List<MessageListVo> messageListItem(HttpServletRequest request) {

        String userId = JwtUtils.getCurrentUserId(request);
        log.info("获取我的消息列表，用户id为：{}", userId);
        List<MyGroupResultVo> myGroupList = groupUserService.getGroupUsersByUserName(userId);
        List<MessageListVo> messageListVos = new ArrayList<>();
        for (MyGroupResultVo myGroupResultVo : myGroupList) {
            GroupMessageResultVo groupLastMessage = groupMessageService.getGroupLastMessage(myGroupResultVo.getGroupId());
            if (groupLastMessage != null && StringUtils.isNoneBlank(groupLastMessage.getMessage())) {
                MessageListVo messageListVo = new MessageListVo();
                messageListVo.setMessageType(groupLastMessage.getMessageType());
                messageListVo.setLastMessage(groupLastMessage.getMessage());
                messageListVo.setId(groupLastMessage.getRoomId());
                messageListVo.setNickname(groupLastMessage.getSenderNickname());
                messageListVo.setAvatar(groupLastMessage.getSenderAvatar());
                messageListVo.setUsername(groupLastMessage.getSenderName());
                messageListVo.setTime(groupLastMessage.getTime());
                messageListVos.add(messageListVo);
            }
            log.info("群聊的最后一条消息:{}", groupLastMessage);
        }
        List<MyFriendListResultVo> myFriendsList = goodFriendService.getMyFriendsList(userId);
        for (MyFriendListResultVo myFriendListResultVo : myFriendsList) {
            System.out.println("myFriendListResultVo:" + myFriendListResultVo);
            System.out.println("myFriendListResultVo.getRoomId():" + myFriendListResultVo.getRoomId());
            System.out.println("getRoomId:" + myFriendListResultVo.getRoomId());
            SingleMessageResultVo lastMessage = singleMessageService.getLastMessageOrNull(myFriendListResultVo.getRoomId());
            System.out.println("lastMessage:" + lastMessage);
            if (lastMessage != null) {
                MessageListVo messageListVo = new MessageListVo();
                messageListVo.setMessageType(lastMessage.getMessageType());
                messageListVo.setLastMessage(lastMessage.getMessage());
                messageListVo.setId(lastMessage.getRoomId());
                String roomId = lastMessage.getRoomId();
                String replace = roomId.replace(userId, "").replace("-", "");
                Optional<User> optional = userDao.findById(replace);
                if (!optional.isPresent()) {
                    throw ExceptionUtil.createException(RestException.class, 1000011);
                }
                User user = optional.get();
                messageListVo.setNickname(user.getNickname());
                messageListVo.setAvatar(user.getAvatar());
                messageListVo.setUsername(user.getUsername());
                messageListVo.setTime(lastMessage.getTime());

                optional = userDao.findById(userId);
                if (!optional.isPresent()) {
                    throw ExceptionUtil.createException(RestException.class, 1000011);
                }
                user = optional.get();
                messageListVo.setFriendBeiZu(user.getFriendBeiZhu().get(replace));
                messageListVos.add(messageListVo);
            }
        }
        Collections.sort(messageListVos);
        return messageListVos;
    }
}
